/*********************************
  串口设置对话框
  ********************************/
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "AdapterSerialPort.h"
#include "qextserialenumerator.h"
#include <QDebug>
settingsDialog::settingsDialog(QWidget *parent) : QDialog(parent)
{
    setupUi(this);
    QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
      foreach( QextPortInfo port, ports ) {
          portNameComboBox->addItem((const QString&)port.portName);
      }
    if(portNameComboBox->currentText()=="LPT1")
      {
        portNameComboBox->setCurrentIndex(1);
    }
    dialogMyPortSettings=new PortSettings();
}

settingsDialog::~settingsDialog()
{
}

void settingsDialog::on_okBtn_clicked()
{
    on_applyBtn_clicked();
}

void settingsDialog::on_applyBtn_clicked()
{
    /*****根据组合框内容对串口进行设置***********/
    //波特率
    if(baudRateComboBox->currentText()==tr("57600"))
    dialogMyPortSettings->BaudRate=BAUD57600;
    else if(baudRateComboBox->currentText()==tr("115200"))
    dialogMyPortSettings->BaudRate=BAUD115200;
    else if(baudRateComboBox->currentText()==tr("600"))
    dialogMyPortSettings->BaudRate=BAUD600;
    else if(baudRateComboBox->currentText()==tr("19200"))
    dialogMyPortSettings->BaudRate=BAUD19200;
    else if(baudRateComboBox->currentText()==tr("9600"))
    dialogMyPortSettings->BaudRate=BAUD9600;
    //数据位
    if(dateBitsComboBox->currentText()==tr("8"))
    dialogMyPortSettings->DataBits=DATA_8;
    else if(dateBitsComboBox->currentText()==tr("7"))
    dialogMyPortSettings->DataBits=DATA_7;
    else if(dateBitsComboBox->currentText()==tr("6"))
    dialogMyPortSettings->DataBits=DATA_6;
    else if(dateBitsComboBox->currentText()==tr("5"))
    dialogMyPortSettings->DataBits=DATA_5;
    //奇偶校验
    if(parityComboBox->currentText()==tr("无"))
    dialogMyPortSettings->Parity=PAR_NONE;
    else if(parityComboBox->currentText()==tr("奇"))
    dialogMyPortSettings->Parity=PAR_ODD;
    else if(parityComboBox->currentText()==tr("偶"))
    dialogMyPortSettings->Parity=PAR_EVEN;
    //停止位
    if(stopBitsComboBox->currentText()==tr("1"))
    dialogMyPortSettings->StopBits=STOP_1;
   // else if(stopBitsComboBox->currentText()==tr("1.5"))
   // dialogMyPortSettings->StopBits=STOP_1_5;
    else if(stopBitsComboBox->currentText()==tr("2"))
    dialogMyPortSettings->StopBits=STOP_2;
    //数据流控制
    if(streamControlComboBox->currentText()==tr("无"))
    dialogMyPortSettings->FlowControl=FLOW_OFF;
    else if(streamControlComboBox->currentText()==tr("Xon/Xoff"))
    dialogMyPortSettings->FlowControl=FLOW_XONXOFF;
    else if(streamControlComboBox->currentText()==tr("硬件"))
    dialogMyPortSettings->FlowControl=FLOW_HARDWARE;
    //timeout设置
    if(timeOutComboBox->currentText()==tr("500"))
    dialogMyPortSettings->Timeout_Millisec=500;
    else if(timeOutComboBox->currentText()==tr("400"))
    dialogMyPortSettings->Timeout_Millisec=400;
    else if(timeOutComboBox->currentText()==tr("100"))
    dialogMyPortSettings->Timeout_Millisec=100;
    else if(timeOutComboBox->currentText()==tr("1000"))
    dialogMyPortSettings->Timeout_Millisec=1000;
    else if(timeOutComboBox->currentText()==tr("5000"))
    dialogMyPortSettings->Timeout_Millisec=5000;
    else if(timeOutComboBox->currentText()==tr("50"))
    dialogMyPortSettings->Timeout_Millisec=50;
    else if(timeOutComboBox->currentText()==tr("10"))
    dialogMyPortSettings->Timeout_Millisec=10;
    else if(timeOutComboBox->currentText()==tr("5"))
    dialogMyPortSettings->Timeout_Millisec=5;
    else if(timeOutComboBox->currentText()==tr("1"))
    dialogMyPortSettings->Timeout_Millisec=1;

    applyBtn->setEnabled(false);;
    emit signalPortSettings(*dialogMyPortSettings);
    emit signalMyComName(portNameComboBox->currentText());
}


/***************端口恢复为默认设置***************/
void settingsDialog::on_setDefaultBtn_clicked()
{
  baudRateComboBox->setCurrentIndex(0);
  dateBitsComboBox->setCurrentIndex(0);
  parityComboBox->setCurrentIndex(0);
  stopBitsComboBox->setCurrentIndex(0);
  streamControlComboBox->setCurrentIndex(0);
  timeOutComboBox->setCurrentIndex(0);
}
